/*
  需求：对已经开封府好的项目中的任何一个类，创建实例时，打印日志信息，输出哪一个类被创建了，并输出传递了哪些参数信息
*/
// 1. 完成日志信息的装饰器
function LoggerInfoDecorator<T extends { new (...args: any): any }>(
  targetClass: T
) {
  /* class LoggerSonClass extends targetClass {
    constructor(...args: any) {
      super(...args);
      console.log("日志信息...targetClass:", targetClass.name);
    }
    methodone() {
      console.log("methodone:", this.name);
    }
  }

  return LoggerSonClass; */

  // 匿名类的使用
  return class extends targetClass {
    constructor(...args: any) {
      super(...args);
      console.log("日志信息...targetClass:", targetClass.name);
    }
    methodone() {
      console.log("methodone:", this.name);
    }
  };
}

// 目标类
@LoggerInfoDecorator
class Test {
  name!: string;
  age!: number;
  // 1. 先执行原来构造函数
  constructor(name: string) {
    this.name = name;
  }

  eat() {
    console.log(this.name, "吃饭");
  }
}

export {};

let test = new Test("abcd");
test.eat();
// test为父类，methodone为子类，此时父类还不知道子类身上有哪些方法
(test as any).methodone();

// let loggerSonClass = LoggerInfoDecorator<typeof Test>(Test);
let loggerSonClass = LoggerInfoDecorator(Test);
let loggerSonClassInstance = new loggerSonClass("张三");
loggerSonClassInstance.methodone();
